package com.carl.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.carl.common.valid.groups.AddGroupsInterface;
import com.carl.common.valid.groups.UpdateGroupsInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.carl.mall.product.entity.BrandEntity;

import com.carl.mall.product.service.BrandService;
import com.carl.common.utils.PageUtils;
import com.carl.common.utils.R;


/**
 * 品牌
 *
 * @author carl
 * @email 541020344@qq.com
 * @date 2024-03-10 00:31:47
 */
@RestController
@RequestMapping("product/brand")
@RefreshScope
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/all")
    public R queryAllBrand() {
        BrandEntity brandEntity = new BrandEntity();
        brandEntity.setName("华为");
        return R.ok().put("brands", brandEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated(AddGroupsInterface.class) @RequestBody BrandEntity brand) {
//        if (result.hasErrors()){
//            HashMap<String, String> map = new HashMap<>();
//            List<FieldError> fieldErrors = result.getFieldErrors();
//            for(FieldError fieldError : fieldErrors){
//                //获取非法数据的字段名
//                String field = fieldError.getField();
//                //获取非法数据的message信息   就是@NotBlank里标注的
//                String defaultMessage = fieldError.getDefaultMessage();
//                map.put(field,defaultMessage);
//            }
//            return R.error(400,"参数有问题重新按规则输入").put("data",map);
//        }
//        brand.setDelFlag(1);
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated(UpdateGroupsInterface.class) @RequestBody BrandEntity brand) {
        //brandService.updateById(brand);
        brandService.updateDetail(brand);
        return R.ok();

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
//        List<BrandEntity> brandEntities = brandService.listByIds(Arrays.asList(brandIds));
//        for (BrandEntity brandEntity : brandEntities) {
//            brandEntity.setShowStatus(0);
//            brandService.updateDetail(brandEntity);
//        }
        brandService.deleteBatchIds(Arrays.asList(brandIds));
        return R.ok();
//    }

    }
}
